<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="上映电影ID" prop="movieId">
      <el-input v-model="dataForm.movieId" placeholder="上映电影ID"></el-input>
    </el-form-item>
    <el-form-item label="演出厅ID" prop="studioId">
      <el-input v-model="dataForm.studioId" placeholder="演出厅ID"></el-input>
    </el-form-item>
    <el-form-item label="放映日期" prop="data">
      <el-date-picker v-model="dataForm.data" placeholder="放映日期"></el-date-picker>
    </el-form-item>
    <el-form-item label="放映时间" prop="time">
      <el-time-picker v-model="dataForm.time" placeholder="放映时间"></el-time-picker>
    </el-form-item>
    <el-form-item label="座位数" prop="seatCount">
      <el-input v-model="dataForm.seatCount" placeholder="座位数"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          movieId: '',
          studioId: '',
          data: '',
          time: '',
          seatCount: ''
        },
        dataRule: {
          movieId: [
            { required: true, message: '上映电影ID不能为空', trigger: 'blur' }
          ],
          studioId: [
            { required: true, message: '演出厅ID不能为空', trigger: 'blur' }
          ],
          data: [
            { required: true, message: '放映日期不能为空', trigger: 'blur' }
          ],
          time: [
            { required: true, message: '放映时间不能为空', trigger: 'blur' }
          ],
          seatCount: [
            { required: true, message: '座位数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/studio/schedule/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.movieId = data.schedule.movieId
                this.dataForm.studioId = data.schedule.studioId
                this.dataForm.data = data.schedule.data
                this.dataForm.time = data.schedule.time
                this.dataForm.seatCount = data.schedule.seatCount
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/studio/schedule/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'movieId': this.dataForm.movieId,
                'studioId': this.dataForm.studioId,
                'data': this.dataForm.data,
                'time': this.dataForm.time,
                'seatCount': this.dataForm.seatCount
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
